/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package javaapplication1;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author abosancic
 */
public class RSMetaData {

    public static void main(String[] args) throws Exception {
        String driver = "oracle.jdbc.driver.OracleDriver";
        Class.forName(driver).newInstance();

        String jdbcUrl = "jdbc:oracle:thin:@localhost:1521:XE";
        Connection conn = DriverManager.getConnection(jdbcUrl, "hr", "hr");

        Statement stmt = conn.createStatement();
        DatabaseMetaData data = conn.getMetaData();
        String SELEC_ALL_TABLES = "SELECT * FROM CAT where TABLE_TYPE = 'TABLE'";

        try {
            ResultSet res = stmt.executeQuery(SELEC_ALL_TABLES);

            while (res.next()) {
                String tableName = res.getString("TABLE_NAME");
                System.out.println("tables: " + tableName);
                Statement stmtByTable = conn.createStatement();
                ResultSet rs = stmtByTable.executeQuery("SELECT * FROM " + tableName + " WHERE ROWNUM<=1");

                printColumnInfo(rs);

//                printColumnNames(rs);
//                processRs(rs);
                stmtByTable.close();
                System.out.println("------------------------------");
            }

        } catch (SQLException e) {
            System.out.println(e.toString());
        }
        
//        ResultSet rs = stmt.executeQuery("SELECT * FROM USERS");
//
//        printColumnInfo(rs);
//
//        printColumnNames(rs);
//        processRs(rs);
//
//        rs = stmt.executeQuery("SELECT * FROM REGIONS");
//        printColumnInfo(rs);

        //printColumnNames(rs);
        //processRs(rs);
        //conn.close();
    }

    public static void processRs(ResultSet rs) throws SQLException {
        ResultSetMetaData rmd = rs.getMetaData();
        while (rs.next()) {
            for (int col = 1; col <= rmd.getColumnCount(); col++) {
                getData(rs, rmd.getColumnType(col), col);
            }
        }
    }

    public static void printColumnNames(ResultSet rs) throws SQLException {
        ResultSetMetaData rmd = rs.getMetaData();
        for (int col = 1; col <= rmd.getColumnCount(); col++) {
            System.out.println(rmd.getColumnName(col) + " ");
        }
    }

    public static void getData(ResultSet rs, int type, int colIdx) throws SQLException {
        switch (type) {
            case java.sql.Types.CHAR:
            case java.sql.Types.VARCHAR:
                System.out.println(rs.getString(colIdx));
                break;

            case java.sql.Types.INTEGER:
                int i = rs.getInt(colIdx);
                System.out.println(i);
                break;

            case java.sql.Types.NUMERIC:
                BigDecimal bd = rs.getBigDecimal(colIdx);
                System.out.println(bd.toString());
                break;

            case java.sql.Types.TIMESTAMP:
            case java.sql.Types.DATE:
                java.sql.Date d = rs.getDate(colIdx);
                System.out.println(d.toString());
                break;

        }
    }

    public static void printColumnInfo(ResultSet rs) throws SQLException {
        ResultSetMetaData rsmd = rs.getMetaData();
        int cols = rsmd.getColumnCount();
        for (int colIdx = 1; colIdx <= cols; colIdx++) {
            String name = rsmd.getColumnName(colIdx);
            int type = rsmd.getColumnType(colIdx);
            String typeName = rsmd.getColumnTypeName(colIdx);
            System.out.println(name + ", " + type + ", " + typeName);
        }
    }
}